namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class FileBLL
    {
        public int Add(File model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into [File](");
            builder.Append("FileName,FilePath,FileSize,FileMD5,AddUser,AddDate,UpdateUser,UpdateDate");
            builder.Append(") values (");
            builder.Append("@FileName,@FilePath,@FileSize,@FileMD5,@AddUser,@AddDate,@UpdateUser,@UpdateDate");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@FileName", model.FileName), new SqlParameter("@FilePath", model.FilePath), new SqlParameter("@FileSize", model.FileSize), new SqlParameter("@FileMD5", model.FileMD5), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@UpdateDate", model.UpdateDate) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int FileID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from [File] ");
            builder.Append(" where FileID=@FileID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4) };
            cmdParms[0].Value = FileID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string FileIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from [File] ");
            builder.Append(" where ID in (" + FileIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<File> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM [File] ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            File item = null;
            List<File> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<File>();
                foreach (DataRow row in table.Rows)
                {
                    item = new File();
                    if (row["FileID"].ToString() != "")
                    {
                        item.FileID = int.Parse(row["FileID"].ToString());
                    }
                    item.FileName = row["FileName"].ToString();
                    item.FilePath = row["FilePath"].ToString();
                    if (row["FileSize"].ToString() != "")
                    {
                        item.FileSize = int.Parse(row["FileSize"].ToString());
                    }
                    item.FileMD5 = row["FileMD5"].ToString();
                    item.AddUser = row["AddUser"].ToString();
                    if (row["AddDate"].ToString() != "")
                    {
                        item.AddDate = DateTime.Parse(row["AddDate"].ToString());
                    }
                    item.UpdateUser = row["UpdateUser"].ToString();
                    if (row["UpdateDate"].ToString() != "")
                    {
                        item.UpdateDate = DateTime.Parse(row["UpdateDate"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public File GetModel(int FileID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select FileID, FileName, FilePath, FileSize, FileMD5, AddUser, AddDate, UpdateUser, UpdateDate  ");
            builder.Append("  from [File] ");
            builder.Append(" where FileID=@FileID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4) };
            cmdParms[0].Value = FileID;
            File file = new File();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["FileID"].ToString() != "")
                {
                    file.FileID = int.Parse(row["FileID"].ToString());
                }
                file.FileName = row["FileName"].ToString();
                file.FilePath = row["FilePath"].ToString();
                if (row["FileSize"].ToString() != "")
                {
                    file.FileSize = int.Parse(row["FileSize"].ToString());
                }
                file.FileMD5 = row["FileMD5"].ToString();
                file.AddUser = row["AddUser"].ToString();
                if (row["AddDate"].ToString() != "")
                {
                    file.AddDate = DateTime.Parse(row["AddDate"].ToString());
                }
                file.UpdateUser = row["UpdateUser"].ToString();
                if (row["UpdateDate"].ToString() != "")
                {
                    file.UpdateDate = DateTime.Parse(row["UpdateDate"].ToString());
                }
                return file;
            }
            return null;
        }

        public bool Update(File model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update [File] set ");
            builder.Append(" FileName = @FileName , ");
            builder.Append(" FilePath = @FilePath , ");
            builder.Append(" FileSize = @FileSize , ");
            builder.Append(" FileMD5 = @FileMD5 , ");
            builder.Append(" AddUser = @AddUser , ");
            builder.Append(" AddDate = @AddDate , ");
            builder.Append(" UpdateUser = @UpdateUser , ");
            builder.Append(" UpdateDate = @UpdateDate  ");
            builder.Append(" where FileID=@FileID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@FileID", model.FileID), new SqlParameter("@FileName", model.FileName), new SqlParameter("@FilePath", model.FilePath), new SqlParameter("@FileSize", model.FileSize), new SqlParameter("@FileMD5", model.FileMD5), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@UpdateDate", model.UpdateDate) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

